class Usuario < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me
  # attr_accessible :title, :body

  has_many :contas, :order => "nome asc"
	has_many :transacoes, :order => "data asc"
	has_many :despesas, :order => "data asc"
	has_many :parcelados, :order => "data asc"
	has_many :parcelamentos, :through => :parcelados, :order => "data asc"

  #
  #
  def contas_ativas
  	contas.where(:ativa => true)
  end

	#
	#
	def despesas_mensais(periodo, somente_quitadas)
		desp_mensais = despesas.where("date_part('month', data) = ? and date_part('year', data) = ?",
			periodo.month, periodo.year)
			
		(somente_quitadas ? desp_mensais.where(:quitada => true) : desp_mensais).order("data asc, valor asc")
	end

	#
	#
	def despesas_quitadas
		despesas.where(:quitada => true).order("data asc, valor asc")
	end

end
